package com.rainxing.cockpit.modules.quartz.controller;

import com.rainxing.cockpit.modules.quartz.service.ScheduledService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @Auther: ybzhu
 * @Date: 2018/7/19 14:16
 * @Description:
 */
@RestController
@RequestMapping("/job")
@Api(value = "定时任务管理", description = "定时任务管理")
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "Successful — 请求已完成"),
        @ApiResponse(code = 400, message = "请求中有语法问题，或不能满足请求"),
        @ApiResponse(code = 401, message = "未授权客户机访问数据"),
        @ApiResponse(code = 403, message = "拒绝或者禁止访问"),
        @ApiResponse(code = 404, message = "服务器找不到给定的资源；文档不存在"),
        @ApiResponse(code = 500, message = "服务器不能完成请求")}
)
@ApiIgnore
public class JobController {
    @Autowired
    private ScheduledService scheduledService;

    @ApiOperation(value = "开始执行", notes = "定时器任务开始执行")
    @GetMapping("quartzStart")
    public String startNNoQuartz() {
        scheduledService.refreshTrigger();

        return "定时器任务开始执行，请注意观察控制台";
    }

    @ApiOperation(value = "暂停任务", notes = "暂停一个定时器任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "job", value = "类名路径", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "group", value = "组别ID", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("pauseJob")
    public String pauseJob(String job, String group) {
        scheduledService.pauseJob(job, group);
        return "暂停一个定时器任务，请注意观察控制台";
    }

    @ApiOperation(value = "暂停重启任务", notes = "暂停重启一个定时器任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "job", value = "类名路径", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "group", value = "组别ID", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("resumeJob") //shutdown关闭了，或者删除了就不能重启了
    public String resumeJob(String job, String group) {
        scheduledService.resumeJob(job, group);
        return "暂停重启一个定时器任务，请注意观察控制台";
    }

    @ApiOperation(value = "删除任务", notes = "删除一个定时器任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "job", value = "类名路径", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "group", value = "组别ID", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("deleteJob")
    public String deleteJob(String job, String group) {
        scheduledService.deleteJob(job, group);
        return "删除一个定时器任务，请注意观察控制台，删除了，重启就没什么用了";
    }

    @ApiOperation(value = "匹配任务", notes = "根据出发规则匹配任务，立即执行定时任务，暂停的时候可以用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "job", value = "类名路径", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "group", value = "组别ID", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("doJob")
    public String doJob(String job, String group) {
        scheduledService.doJob(job, group);
        return "根据出发规则匹配任务，立即执行定时任务，暂停的时候可以用";
    }

    @ApiOperation(value = "开启定时器", notes = "开启定时器，这时才可以开始所有的任务，默认是开启的")
    @GetMapping("startAllJob")
    public String startAllJob() {
        scheduledService.startAllJob();
        return "开启定时器，这时才可以开始所有的任务，默认是开启的";
    }

    @ApiOperation(value = "关闭任务", notes = "关闭定时器，则所有任务不能执行和创建")
    @GetMapping("shutdown")
    public String shutdown() {
        scheduledService.shutdown();
        return "关闭定时器，则所有任务不能执行和创建";
    }
}
